import * as zrender from 'zrender';
import {
  barHeight,
  barMargin,
  unitWidth,
} from '../../config';
// import { createLeftArrowRect, createRightArrowRect } from "./create-arrow";
import { getUniquePersons } from '../../utils/helper';
import { getShiftTypeStroke } from './shift-type-stroke';
import { addScheduleText } from './shift-type-label';
import { setupScheduleEvents } from './dragging';
import { setupResizeHandles } from './handle-bar';

// 排班条的纵向间距配置
const SCHEDULE_BAR_VERTICAL_PADDING = 3;
const SCHEDULE_BAR_HEIGHT = barHeight - (SCHEDULE_BAR_VERTICAL_PADDING * 2);

/**
 * 绘制排班条
 * @param zr ZRender实例
 * @param schedules 人员排班数据
 * @param chartState 图表状态对象
 * @param redrawChart 重绘图表函数
 * @param currentGroup 当前操作组
 * @returns 绘制的排班条数量
 */
export function drawScheduleBars(
  zr: any,
  schedules: PersonSchedule[],
  chartState: ChartState,
  redrawChart: RedrawChartFunction,
  currentGroup: any,
) {
  let drawShiftCount = 0;
  const boundingLeft = Math.floor(chartState.lastScrollX / unitWidth);
  const boundingRight = Math.floor((chartState.lastScrollX + chartState.canvasWidth) / unitWidth);

  // 获取去重后的人员列表（与drawPersonColumn保持一致）
  const uniquePersons = getUniquePersons(schedules);

  uniquePersons.forEach((schedule, personIndex) => {
    // 性能优化：在视口外跳过
    if (personIndex > Math.floor((chartState.canvasHeight - chartState.chartStartY) / (barHeight + barMargin))) {
      return;
    }

    const personY = chartState.chartStartY + (barHeight + barMargin) * personIndex;

    // 绘制该人员的所有排班条
    schedule.shifts.forEach((shift, shiftIndex) => {
      // 计算位置和尺寸
      const x = chartState.chartStartX + shift.start * unitWidth;
      const y = personY + SCHEDULE_BAR_VERTICAL_PADDING;
      const width = shift.duration * unitWidth;

      // const taskBarRect = {
      //   width,
      //   height: barHeight
      // };
      // 创建左右箭头
      // const leftArrow = createLeftArrowRect(x, y, shift, taskBarRect, true, boundingLeft, function () {
      //   redrawChart(true, (shift.start - 3) * unitWidth, 0);
      // });
      // const rightArrow = createRightArrowRect(x, y, shift, unitWidth, chartState.lastScrollX, chartState.canvasWidth, taskBarRect, true, boundingRight, function () {
      //   redrawChart(true, (shift.start + shift.duration + 3) * unitWidth - chartState.canvasWidth, 0);
      // });
      // leftArrow && zr.add(leftArrow);
      // rightArrow && zr.add(rightArrow);

      // 如果排班条在视口外，跳过
      if (shift.start > boundingRight || (shift.start + shift.duration) < boundingLeft) {
        return;
      }

      drawShiftCount++;

      // 创建排班条组
      const group = createScheduleGroup(x, y, width, shift, personIndex, shiftIndex, chartState, uniquePersons, redrawChart, currentGroup, zr);
      zr.add(group);
    });
  });

  return drawShiftCount;
}

/**
 * 创建排班条组
 * @param x X坐标
 * @param y Y坐标
 * @param width 宽度
 * @param shift 排班对象
 * @param personIndex 人员索引
 * @param shiftIndex 排班索引
 * @param chartState 图表状态对象
 * @param schedules 人员排班数据
 * @param redrawChart 重绘图表函数
 * @param currentGroup 当前操作组
 * @param zr ZRender实例
 * @returns 排班条组
 */
function createScheduleGroup(
  x: number,
  y: number,
  width: number,
  shift: Shift,
  personIndex: number,
  shiftIndex: number,
  chartState: ChartState,
  schedules: PersonSchedule[],
  redrawChart: RedrawChartFunction,
  _currentGroup: any,
  _zr: any,
) {
  const group = new zrender.Group({
    x,
    y,
    draggable: true,
  });

  // 设置组的索引信息
  (group as any).personIndex = personIndex;
  (group as any).shiftIndex = shiftIndex;

  // 创建排班矩形
  const rect = new zrender.Rect({
    shape: {
      x: 0,
      y: 0,
      width,
      height: SCHEDULE_BAR_HEIGHT,
      r: 6,
    },
    style: {
      fill: shift.fillColor,
      stroke: getShiftTypeStroke(shift.type),
      lineWidth: 2,
    },
    cursor: 'move',
  });
  group.add(rect);

  // 添加排班文字
  addScheduleText(group, shift, width);

  // 创建调整大小手柄
  const handleWidth = 8;

  // 左侧调整手柄
  const leftHandle = new zrender.Rect({
    shape: {
      x: 0,
      y: 0,
      width: handleWidth,
      height: SCHEDULE_BAR_HEIGHT,
      r: [6, 0, 0, 6],
    },
    style: {
      fill: 'transparent',
      stroke: 'transparent',
      lineWidth: 0,
    },
    cursor: 'ew-resize',
    z: 10,
    draggable: false,
  });

  // 为左侧手柄添加事件阻止
  leftHandle.on('click', (e: any) => {
    e.event.stopPropagation();
  });
  leftHandle.on('dragstart', (e: any) => {
    e.event.stopPropagation();
  });

  // 右侧调整手柄
  const rightHandle = new zrender.Rect({
    shape: {
      x: width - handleWidth,
      y: 0,
      width: handleWidth,
      height: SCHEDULE_BAR_HEIGHT,
      r: [0, 6, 6, 0],
    },
    style: {
      fill: 'transparent',
      stroke: 'transparent',
      lineWidth: 0,
    },
    cursor: 'ew-resize',
    z: 10,
    draggable: false,
  });

  // 为右侧手柄添加事件阻止
  rightHandle.on('click', (e: any) => {
    e.event.stopPropagation();
  });
  rightHandle.on('dragstart', (e: any) => {
    e.event.stopPropagation();
  });

  // 设置调整手柄的事件
  setupResizeHandles(leftHandle, rightHandle, group, rect, shift, personIndex, shiftIndex, schedules, redrawChart, chartState);

  // 添加手柄到组
  group.add(leftHandle);
  group.add(rightHandle);

  // 设置事件处理
  setupScheduleEvents(group, rect, shift, personIndex, shiftIndex, chartState, schedules, redrawChart);

  return group;
}
